﻿/*
 *	jquery.msgbox 3.1 - 2009-10-03
 *
 *	All the stuff written by pwwang (pwwang.com)	
 *	Feel free to do whatever you want with this file
 *
 */
(function($) {
	$.msgbox = function(o) {
		
		if(typeof(o) == 'string'){ o = { content:o } }
		var options = o || {};
		
		options.width 			= o.width 		|| 360;  
		options.height 			= o.height 		|| 200, 
		options.closeAfter 		= o.closeAfter 	|| 0;
		options.title 			= o.title 		|| '提示', // title of alert
		options.wrapperClass	= o.wrapperClass|| 'alert_wrapper';
		options.titleClass 		= o.titleClass 	|| 'alert_title';
		options.titleWrapperClass = o.titleWrapperClass || 'alert_title_wrapper';
		// update: revise the bug that font of title can not be setted
		options.mainClass 		= o.mainClass 	|| 'alert_main';
		options.bgClass 		= o.bgClass 	|| 'alert_bg';
		options.type 			= o.type 		|| 'text'; // support  text url(=get) iframe
		options.content 		= o.content 	|| 'Hello, world!';
		options.closeEvent 		= o.closeEvent 	|| function(){};
		options.closeImg		= o.closeImg	|| 'Images/close.gif';
		options.bgOpacity		= o.bgOpacity	|| 0.6;   // from 0 to 1
		options.afterAjaxEvent	= o.afterAjaxEvent|| function(){};
		options.oneBoxMode		= typeof(o.oneBoxMode) == 'undefined' ? true : o.oneBoxMode;
		
		var dragFlag = false;
		var startX = 0;
		var startY = 0;
		
		var $background = $(document.createElement("div"))
			.css({
				 'position'	: 'absolute',
				 'top'		: '0',
				 'left'		: '0',
				 'z-index'	: '9999'
			})
			.addClass(options.bgClass)
			.appendTo('body')
			.animate({opacity:options.bgOpacity})
			.dblclick(closeMe)
			.click(function(){
				flashTitle(0.5, 4, 80);
			})
			.mouseup(dragEnd);
			
		// help IE users if possible
		try {
			$background.bgiframe();
		} catch(e) { }	
		
		var $wrapper = $(document.createElement("div"))
			.css({
				'width' 	: options.width + 'px',
				'height' 	: options.height + 'px',
				'position' 	: 'absolute',
				'z-index'	: '10000'
			})
			.addClass(options.wrapperClass)
			.appendTo('body')
			.mouseup(dragEnd);
		
		var $titleWrapper = $(document.createElement("div"))
			.addClass(options.titleWrapperClass)
			.appendTo($wrapper);
		$titleWrapper.css({
			 'cursor' : 'move',
			 //'display' : 'table', // 3.1
			 'vertical-align' : 'middle',
			 'width' : ( options.width
						- $wrapper.outerWidth()
						+ $wrapper.width() )
						+ 'px'
			})
			.select(function(e){
				if(e.preventDefault)
					e.preventDefault(); 
				if(e.stopPropagation())
					e.stopPropagation();
				return false;				 
			})
			.mousedown(function(e){
				dragStart(e);					
			})
			.mousemove(function(e){
				if(dragFlag)
					dragTo(startX, startY, e);				
			})
			.mouseout(function(e){
				if(dragFlag)
					dragTo(startX, startY, e);			   
			})
			.mouseup(dragEnd);
			
		var $title = $(document.createElement("span"))
			.html(options.title)
			.appendTo($titleWrapper)
			.addClass(options.titleClass)
			.select(function(e){
				if(e.preventDefault)
					e.preventDefault(); 
				if(e.stopPropagation())
					e.stopPropagation();
				return false
			})
			.mouseup(dragEnd);
			
		var $close = $(document.createElement("img"))
			.attr({
				'src': options.closeImg,
				'valign': 'absmiddle'
			})
			.appendTo($titleWrapper)
			.mousedown(closeMe);
			
		$close.css({
			'cursor' : 'pointer',
			'vertical-align' : 'middle',
			'float' : 'left',
			'margin-top' : ($titleWrapper.height() 
							- $close.outerHeight() )/2
							+ 'px'
			//make the image in the middle of title wrapper
		});	

		$title.css({
			'float' : 'left',
			//'display' : 'table-cell', 3.1
			'width' : ( options.width   //calculate the width of $title
						- $close.outerWidth(true) 
						- $titleWrapper.outerWidth() 
						+ $titleWrapper.width()
						- $title.outerWidth(true) 
						+ $title.width() -2 ) + 'px'
		});
			
		var $main = $(document.createElement("div"))
			.addClass(options.mainClass)
			.appendTo($wrapper);
			
		$main.height( 
			options.height 
				- $titleWrapper.outerHeight(true)
				- $main.outerHeight(true)
				+ $main.height()
			)
			.mouseup(dragEnd);

		function closeMe(){
			$background.fadeOut();
			$wrapper.fadeOut();
			options.closeEvent();
			setTimeout(removeMe,3000);  // version 3.1
		}
		
		function removeMe(){
			$title.remove();
			$close.remove();
			$titleWrapper.remove();
			$main.remove();
			$background.remove();
			$wrapper.remove();				
		}
		
		function isVisible(){
			return	$background.is(":visible") &&
					$wrapper.is(":visible");
		}

		function autoCloseMe(closeAfter){			
			if( closeAfter > 0 && isVisible() ){ // prevent manually closing
				autoCloseStr = closeAfter + " 秒后关闭 ...";
				$title.html(options.title + " &nbsp; " + autoCloseStr);		
				closeAfter --;
				if( closeAfter == 0 ) 
					closeMe();	
				setTimeout(function(){ autoCloseMe(closeAfter) }, 1000);	
			}		
		}
		
		function dragStart(e){			
			if(e.preventDefault)
				e.preventDefault(); 
			if(e.stopPropagation())
				e.stopPropagation();			
			dragFlag = true;
			var offset = $wrapper.offset();
			startX = e.pageX - offset.left;
			startY = e.pageY - offset.top;				
		}
		
		function dragTo(x, y, e){		
			if(e.preventDefault)
				e.preventDefault(); 
			if(e.stopPropagation())
				e.stopPropagation();			
			ensureMoveInScreen(e.pageX-x, e.pageY-y);
		}
		
		function ensureMoveInScreen(x,y){		
			var offset = $wrapper.offset();
			if( x < 0 ) boxMoveTo(0, y);
			if( y < 0 ) boxMoveTo(x, 0);
			if( offset.left > $(window).width() - options.width ) 
				boxMoveTo($(window).width() - options.width, y);
			if( offset.top > $(window).height() - options.height )
				boxMoveTo(x, $(window).height() - options.height);
			if( x>=0 && x<=$(window).width()-options.width &&
				y>=0 && y<=$(window).height()-options.height)
				boxMoveTo(x,y);		
		}
		
		function boxMoveTo(x,y){
			$wrapper.css({
				'top'  : y + 'px',
				'left' : x + 'px'
			});		
		}
		
		function dragEnd(){					
			var offset = $wrapper.offset();
			ensureMoveInScreen(offset.left, offset.top);	
			dragFlag = false;
		}
		
		function resetPosition() {
			$background.css({
				 'width'	: $(window).width() + 'px',
				 'height'	: ( $('body').height() > $(window).height() 
								? $('body').height() 
								: $(window).height() ) + 'px'  //update
			});
			$wrapper.css({
				'top'		: ($(window).height() - options.height)/2 + 'px',
				'left'	 	: ($(window).width() - options.width)/2 + 'px'			 
			});	
		}
		
		function flashTitle(opacity, times, interval, flag){
			if( times > 0 ) {
				flag = !flag;
				op = flag ? opacity : 1;
				$titleWrapper.css('opacity',op);	
				setTimeout(function(){ flashTitle(opacity, times-1, interval, flag) }, interval);
			}
		}
		
		var timeOut;
		function bindIframeMouseup(index){ // for dragEnd, if mouse released in the iframe
			try {
				window.frames[index].document.onmouseup = dragEnd;
			} catch(e) {}
			timeOut = setTimeout(function(){bindIframeMouseup(index)}, 200);
		}
		
		function msgbox(){		
			switch(options.type){
				case 'text':
					$main.html(options.content);
					if(self!=top && options.oneBoxMode) { 
						removeMe();
						$('body').html($main);  //version 3.1
						//window.open(options.content, '_self', '');
						//setTimeout(function(){window.location.href = options.content;},1);
						// bug for ie 8
					} 
					break;			
				case 'get':
				case 'ajax':
				case 'url':
					$main.text("Loading ...").load(
						options.content,
						'',
						function(data){
							(options.afterAjaxEvent)(data);	
						}
					);	
					break;			
				case 'iframe':	// attr can not hide border in IE8
					if(self!=top && options.oneBoxMode) { 
						removeMe();
						//window.open(options.content, '_self', '');
						setTimeout(function(){window.location.href = options.content;},1);
						// bug for ie 8
					} else { // mark sure this page is not in an iframe
						var $iframe = $('<iframe frameborder="0" marginheight="0" marginwidth="0"></iframe>')
							.appendTo($main)
							.attr({
								'width'			: '100%',
								'height'		: '100%',
								'scrolling' 	: 'auto',
								'src'			: options.content
							});
							//.appendTo($main);   a bug for IE, iframe not show	
						$iframe.find(document)
							.ready(function(){
								var index = $("iframe").index($iframe);
								bindIframeMouseup(index);
								clearTimeout(setTimeout(function(){bindIframeMouseup(0)},200));
							});
							bindIframeMouseup($iframe);
							clearTimeout(timeOut);
				
					}
					break;
			}	
		}
		
		resetPosition();
		$(window)
			.load(resetPosition)		// just in case user is changing size of page while loading
			.resize(resetPosition)
			.mouseup( dragEnd );	
		if( options.closeAfter > 0 ) 
			autoCloseMe(options.closeAfter);
		msgbox();

		return this;
	}			
})(jQuery);
